package co2.bean;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.DAOInterface;
import co2.pojos.HasilMotivasi;
import co2.pojos.HasilTest;
import co2.pojos.MstUser;
import co2.pojos.PaketMotivasi;
import co2.pojos.PaketSoal;


@ManagedBean
@ViewScoped
public class HasilMotivasiBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private List<HasilMotivasi> list;
	private List<PaketMotivasi> listPs;
	private List<MstUser> listUser;
	private String psId;
	private String userId;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	
	@PostConstruct
	public void postCon(){
		listPs = dao.getAllPaketMotivasi();
		listUser = dao.getAllMstUser();
		
		search();
		
	}
	public void delete(){
		try{
			list = dao.getAllHasilMotivasiByUserIdAndPaket(userId, psId);
			if(list!=null&&list.size()>0){
				for(HasilMotivasi hm:list){
					dao.delete(HasilMotivasi.class, hm.getId());
				}
				search();
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Item Deleted",""));
			}else{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"No records to delete",""));
			}
			
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Delete Fail",""));

		}
	}
	public void search(){
		if(psId!=null){
			list = dao.getAllHasilMotivasiByUserIdAndPaket(userId, psId);
		}
	}

	public List<HasilMotivasi> getList() {
		return list;
	}

	public void setList(List<HasilMotivasi> list) {
		this.list = list;
	}

	public List<PaketMotivasi> getListPs() {
		return listPs;
	}

	public void setListPs(List<PaketMotivasi> listPs) {
		this.listPs = listPs;
	}

	public List<MstUser> getListUser() {
		return listUser;
	}

	public void setListUser(List<MstUser> listUser) {
		this.listUser = listUser;
	}

	public String getPsId() {
		return psId;
	}

	public void setPsId(String psId) {
		this.psId = psId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}

	
	
}